<?php

namespace app\api\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Validate;

class Addonshop extends Controller
{


    /**
     * new 新闻动态相册 图集
     */
    public function news()
    {
        $dataArray = Db::name('addonarticle')
            ->alias('s')
            ->select();
//        $result = array('errCode' => count($dataArray), 'errMsg' => '测试!', 'data' => $dataArray);
//        return json_encode($result);die;

        $data = [];
        $paramArrayaaa = array();
        $typenameArray = array();
        $huxinNum = 0;
        $albumNum = 0;
        $otherNum = 0;
        foreach ($dataArray as $key => $val) {
//            echo '每个aid================================================================================<br/>';
            $dataEach = [];
            $dataEach['aid'] = $val['aid'];
            $dataEach['typeid'] = $val['typeid'];
            if (empty($val['imgurls'])) {
//                dump($val['imgurls']);
                continue;
            }
//            dump($val);

//            $imgnums = substr_count($val['imgurls'], '{dede');
            $pose = explode('{dede:img', $val['imgurls']);
            foreach ($pose as $i => $v) {
//                echo 'aid下imgurls每个dede标签-----------------------------------------<br/>';
                $v = '{dede:img' . $v;

                $imgurlsArr[] = [];
                $explodeV = explode(' ', $v);
                $paramObj = array();
                foreach ($explodeV as $k => $j) {
                    $domain1 = strstr($j, '=');
                    $domainJpg = strstr($j, '.jpg');
                    if ($domain1) {
                        $j = str_replace('}', '', $j);
                        $j = str_replace("'", '', $j);
                        $paramArr = explode('=', $j);
                        if ($paramArr[1] == "''") {

                        } elseif ($paramArr[1] == '"\'\'/') {

                        } else {
                            $paramObj[$paramArr[0]] = $paramArr[1];
                            $paramArrayaaa[$paramArr[0]] = $paramArr[0];
                        }
                    } else if ($domainJpg && !$domain1) {
//                echo $j.'：包含jpg的且不包含=的';
                        $paramObj['delimg'] = $j;
                    }
                }

                if ($paramObj) {
                    if (empty($paramObj['ddimg'])) {
                        continue;
                    }
                    if (empty($paramObj['text'])) {
                        $paramObj['text'] = '';
                    }
                    if (empty($paramObj['text2'])) {
                        $paramObj['text2'] = '';
                    }
                    if (empty($paramObj['ddimg'])) {
                        $paramObj['ddimg'] = '';
                    } else {
                        $paramObj['ddimg'] = (string)$paramObj['ddimg'];
                    }
                    if (empty($paramObj['delimg'])) {
                        $paramObj['delimg'] = '';
                    }
                    if (empty($paramObj['video_url'])) {
                        $paramObj['video_url'] = '';
                    }
                    if (empty($paramObj['vrurl'])) {
                        $paramObj['vrurl'] = '';
                    }
                    if (empty($paramObj['width'])) {
                        $paramObj['width'] = 0;
                    }
                    if (empty($paramObj['height'])) {
                        $paramObj['height'] = 0;
                    }
                    if($val['typename']=='项目户型'){
                        $imgtype = 1;
                    }elseif($val['typename']=='项目相册'){
                        $imgtype = 2;
                    }else{
                        $imgtype = 3;
                    }
                    $insetData = [
                        "aid"=> $val['aid'],
                        'typeid'=>$val['typeid'],
                        'typename'=>$val['typename'],
                        'img_type'=>$imgtype,
                        'img_url'=> $paramObj['ddimg'],
                        'delimg' => $paramObj['delimg'],
                        'video_url'=>$paramObj['video_url'],
                        'vr_url'=>$paramObj['vrurl'],
                        'note1'=>$paramObj['text'],
                        'note2'=>$paramObj['text2'],
                        'width'=>$paramObj['width'],
                        'height'=>$paramObj['height'],
                        'create_time'=>time()
                    ];

                    $result['album'][]= $paramObj;
                    $InsertId = Db::name('addonshop_album')
                        ->insertGetId($insetData);
                    $InsertId1 = Db::name('addonshop_album')
                        ->where('id',$InsertId)
                        ->update(['rank'=>$InsertId]);
//
                    if (!$InsertId1) {
                        $result = array('errCode' => $InsertId1, 'errMsg' => '', 'data' => $paramObj);
                    }
                }
            }

        }
//        dump($typenameArray);
//        die;
        $result['count'] = count($data);
        $result['product'] = $data;
        $result['data'] = $dataArray;


        $result = array('errCode' => 0, 'errMsg' => '测试!', 'data' => $result);
        return json_encode($result);

    }









    /**
     * new 项目相册 图集
     */
    public function project()
    {
        $dataArray = Db::name('addonshop')
            ->alias('s')
            ->join('arctype t','s.typeid=t.id','left')
            ->where('s.imgurls', "neq", '')
            ->field('s.aid,s.imgurls,s.typeid,t.typename')
            ->select();
        $data = [];
        $paramArrayaaa = array();
        $typenameArray = array();
        $huxinNum = 0;
        $albumNum = 0;
        $otherNum = 0;
        foreach ($dataArray as $key => $val) {


//            echo '每个aid================================================================================<br/>';
            $dataEach = [];
            $dataEach['aid'] = $val['aid'];
            $dataEach['typeid'] = $val['typeid'];
            if (empty($val['imgurls'])) {
//                dump($val['imgurls']);
                continue;
            }
//            dump($val);

            $imgnums = substr_count($val['imgurls'], '{dede');
            $pose = explode('{dede:img', $val['imgurls']);
            foreach ($pose as $i => $v) {
//                echo 'aid下imgurls每个dede标签-----------------------------------------<br/>';
                $v = '{dede:img' . $v;

                $imgurlsArr[] = [];
                $explodeV = explode(' ', $v);
                $paramObj = array();
                foreach ($explodeV as $k => $j) {
                    $domain1 = strstr($j, '=');
                    $domainJpg = strstr($j, '.jpg');
                    if ($domain1) {
                        $j = str_replace('}', '', $j);
                        $j = str_replace("'", '', $j);
                        $paramArr = explode('=', $j);
                        if ($paramArr[1] == "''") {

                        } elseif ($paramArr[1] == '"\'\'/') {

                        } else {
                            $paramObj[$paramArr[0]] = $paramArr[1];
                            $paramArrayaaa[$paramArr[0]] = $paramArr[0];
                        }
                    } else if ($domainJpg && !$domain1) {
//                echo $j.'：包含jpg的且不包含=的';
                        $paramObj['delimg'] = $j;
                    }
                }

                if ($paramObj) {
                    if (empty($paramObj['ddimg'])) {
                        continue;
                    }
                    if (empty($paramObj['text'])) {
                        $paramObj['text'] = '';
                    }
                    if (empty($paramObj['text2'])) {
                        $paramObj['text2'] = '';
                    }
                    if (empty($paramObj['ddimg'])) {
                        $paramObj['ddimg'] = '';
                    } else {
                        $paramObj['ddimg'] = (string)$paramObj['ddimg'];
                    }
                    if (empty($paramObj['delimg'])) {
                        $paramObj['delimg'] = '';
                    }
                    if (empty($paramObj['video_url'])) {
                        $paramObj['video_url'] = '';
                    }
                    if (empty($paramObj['vrurl'])) {
                        $paramObj['vrurl'] = '';
                    }
                    if (empty($paramObj['width'])) {
                        $paramObj['width'] = 0;
                    }
                    if (empty($paramObj['height'])) {
                        $paramObj['height'] = 0;
                    }
                    if($val['typename']=='项目户型'){
                        $imgtype = 1;
                    }elseif($val['typename']=='项目相册'){
                        $imgtype = 2;
                    }else{
                        $imgtype = 3;
                    }
                    $insetData = [
                        "aid"=> $val['aid'],
                        'typeid'=>$val['typeid'],
                        'typename'=>$val['typename'],
                        'img_type'=>$imgtype,
                        'img_url'=> $paramObj['ddimg'],
                        'delimg' => $paramObj['delimg'],
                        'video_url'=>$paramObj['video_url'],
                        'vr_url'=>$paramObj['vrurl'],
                        'note1'=>$paramObj['text'],
                        'note2'=>$paramObj['text2'],
                        'width'=>$paramObj['width'],
                        'height'=>$paramObj['height'],
                        'create_time'=>time()
                    ];

                    $result['album'][]= $paramObj;
                    $InsertId = Db::name('addonshop_album')
                        ->insertGetId($insetData);
                    $InsertId1 = Db::name('addonshop_album')
                        ->where('id',$InsertId)
                        ->update(['rank'=>$InsertId]);
//
                    if (!$InsertId1) {
                        $result = array('errCode' => $InsertId1, 'errMsg' => '', 'data' => $paramObj);
                    }
                }
            }

        }
//        dump($typenameArray);
//        die;
        $result['count'] = count($data);
        $result['product'] = $data;
        $result['data'] = $dataArray;


        $result = array('errCode' => 0, 'errMsg' => '测试!', 'data' => $result);
        return json_encode($result);

    }
}
